package com.avic.rong.utils.permission;

import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;


/**
 * 权限管理
 *
 * @author LiuKai
 */
public class PermissionManager {

    public static final String TAG = PermissionManager.class.getSimpleName();

    /**
     * 私有化构造方法
     */
    private PermissionManager() {
    }

    private static class SingletonHolder {
        private static final PermissionManager instance = new PermissionManager();
    }

    public static PermissionManager getInstance() {
        return SingletonHolder.instance;
    }


    /**
     * 申请多个权限。成功或者失败的权限名称会一个个返回
     *
     * @param fragmentActivity   activity中进行授权
     * @param permissionCallBack 权限申请接口回掉
     * @param permissionNames    权限名称(多个 , 分割)
     */
    public void requestEach(FragmentActivity fragmentActivity, final PermissionCallBack permissionCallBack, String... permissionNames) {

        if (fragmentActivity == null) {
            return;
        }

        if (permissionNames == null) {
            Log.e(TAG, "permissionNames 不能为空!");
            return;
        }

        RxPermissions rxPermissions = new RxPermissions(fragmentActivity);
        rxPermissions.requestEach(permissionNames).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {

                if (permission.granted) {
                    if (permissionCallBack != null) {
                        permissionCallBack.onGranted(permission.name);
                    }
                } else if (permission.shouldShowRequestPermissionRationale) {
                    if (permissionCallBack != null) {
                        permissionCallBack.onDenied(permission.name);
                    }
                } else {
                    if (permissionCallBack != null) {
                        permissionCallBack.onDeniedWithNeverAsk(permission.name);
                    }
                }


            }
        });

    }


    /**
     * 申请多个权限。成功或者失败，权限名称会全部返回
     *
     * @param fragmentActivity   activity中进行授权
     * @param permissionCallBack 权限申请接口回掉
     * @param permissionNames    权限名称(多个 , 分割)
     */
    public void requestEachCombined(FragmentActivity fragmentActivity, final PermissionCallBack permissionCallBack, String... permissionNames) {
        if (fragmentActivity == null) {
            return;
        }

        if (permissionNames == null) {
            Log.e(TAG, "permissionNames 不能为空!");
            return;
        }

        RxPermissions rxPermissions = new RxPermissions(fragmentActivity);
        rxPermissions.requestEachCombined(permissionNames).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {
                if (permission.granted) {
                    if (permissionCallBack != null) {
                        permissionCallBack.onGranted(permission.name);
                    }
                } else if (permission.shouldShowRequestPermissionRationale) {
                    if (permissionCallBack != null) {
                        permissionCallBack.onDenied(permission.name);
                    }
                } else {
                    if (permissionCallBack != null) {
                        permissionCallBack.onDeniedWithNeverAsk(permission.name);
                    }
                }
            }
        });

    }


}
